/**
 * \file kernel/basic_utils.h
 * This file includes several misc utilies for the library
 * */

#ifndef DODO_PLOT_BASIC_UTILS_H
#define DODO_PLOT_BASIC_UTILS_H

#include <dodo_plot/kernel/plotfwd.h>
#include <dodo_plot/kernel/basic_elements.h>
#include <string>
#include <sstream>

namespace dodo_plot
{

/// Sets Cairo::Context's source color with dodo_plot::Color
/// This is only used when using Cairo stuff directly, instead of using dodo_plot::Plotter
void set_cr_color(Context_p& cr, const Color& color);

/// A class of functor, which converts local point location to a point on the given canvas.
class Pos_Projection
{
public:
	/// Takes a const Point& to convert
	inline Point operator() (const Point& p) const
	{
		return Point(p.x * x_scale_ + x_offset_,
			p.y * y_scale_ + y_offset_);
	}

	/// Takes two double values to convert
	inline Point operator() (double x, double y) const 
	{
		return Point(x * x_scale_ + x_offset_,
			y * y_scale_ + y_offset_);
	}

	Pos_Projection();

	/// When using this ctor, the local position is assumed (0, local_w, 0, local_w).
	/// If this is not the case, use the ctor that takes two Position instead.
	Pos_Projection(const Position& given_pos, double local_w, double local_h);
	Pos_Projection(const Position& given_pos, const Position& local_pos);
private:
	double x_offset_;
	double y_offset_;
	double x_scale_;
	double y_scale_;
};

/// converts anything that is streamable to string.
template <class T>
std::string to_string(const T& val)
{
	std::stringstream ss;
	ss << val;
	return ss.str();
}

}


#endif //DODO_PLOT_BASIC_UTILS_H

